import QtQuick 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"


FocusScope {
    id : mainIt
    width: 1024
    height: 700
    focus: true
    states: [	
	      State { 
		name: "episodeView"
		AnchorChanges {target: etape;anchors.right : mainIt.left; anchors.left:''}
	      }
	    ,State { 
		name: "default"
		AnchorChanges {target: etape;anchors.left:mainIt.left; anchors.right :''}
	      }
	    ]

    transitions: Transition {
      AnchorAnimation { duration: 400 }
    }
    
    Keys.onPressed: {
	      console.log("key:" + event.key);
	      if (event.key == Qt.Key_Backspace) {
		console.log("retour");
		m6Invoke.write('r');
		event.accepted = true;
	    }
    }
    Episode {
	  id:episode
	  anchors.left:etape.right
	  width: parent.width
	  height: parent.height
	  onChoiceLink : {
	      console.log('choix utilisateur episode:'+ lien);
	      vlcPlayer.shellAction ('vlc',['--fullscreen',lien]) ;
	  }
    }
    Etape {
	    id:etape
	    width: parent.width
	    height: parent.height
	    focus:true
	    onChoice : {
		    console.log('choix utilisateur:'+ index);
		    m6Invoke.write(index);
	    }
    }
    
	
  Component.onCompleted: {
	  mainIt.state='default';
	//m6Invoke.shellAction('python',['-c', 'print "hhhhhhh"']);
	  m6Invoke.shellAction('python',['-u','/socMDC/KdeadMouse/common/bin/tvdownloader/mainCli.py']);
  }
	  
  ShellInvoker {
      id : vlcPlayer
      onFinished : {
	console.log('finish vlcPlayer');
      }
      onOutputLineChanged : {
	console.log('vlcPlayer:' + outputLine);
      }
  }
  
  ShellInvoker {
    id : m6Invoke
    onFinished : {
      console.log('finish m6Invoke');
    }
    
    onOutputLineChanged : {
      console.log('outputLine:'+outputLine);
      try{
	      var jout = eval('('+outputLine+')');
	      if(jout.niveau=='fichiers'){
		//charger episode
		episode.loadData(jout.data);
		mainIt.state = 'episodeView'
		episode.focus=true;
	      }else{
		vlcPlayer.kill();
		etape.loadData(jout);
		mainIt.state='default';
		etape.focus=true;
	      }
      }catch(e){
	      //console.log('eval ('+outputLine+') failed:'+e);
      }
      //loadData({"path":"/", "data": ["Info","Series ","Emissions","Jeunesse"]});
    }
  }
}